/**
*      author: Y_Yao
*      created: 2025-04-07 13:16:58
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n,p;

int my_pow(int a,int b)
{
    int ans = 1;
    while(b > 0)
    {
        if(b & 1) ans = (ans * a) % mod;
        a = a * a % mod;
        b = b >> 1;
    }
    return ans % mod;
}

void solve()
{
    cin >> n >> p;
    int ans = ((p - 1) * (my_pow(p-2,n-1)) % mod) % mod;
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}